<template>
  <el-dialog title="新增员工" :visible="showDialog" @close="btnCancel">
    <el-form ref="addEmployee" label-width="120px" :model="formData" :rules="rules">
      <!-- 角色栏 -->
      <el-form-item label="姓名" prop="username">
        <el-input v-model="formData.username" style="width:400px" placeholder="1-30个字符" />
      </el-form-item>
      <!-- 手机栏 -->
      <el-form-item label="手机" prop="mobile">
        <el-input v-model="formData.mobile" style="width:400px" placeholder="请输入手机号" />
      </el-form-item>
      <!-- 入职时间栏 -->
      <el-form-item label="入职时间" prop="timeOfEntry">
        <el-date-picker
          v-model="formData.timeOfEntry"
          placeholder="选择日期"
        />
      </el-form-item>
      <!-- 聘用形式栏 -->
      <el-form-item label="聘用形式" prop="formOfEmployment">
        <el-select v-model="formData.formOfEmployment" placeholder="请选择">
          <el-option v-for="item in EmployeeEnum.hireType" :key="item.id" :label="item.value" :value="item.id" />
        </el-select>
      </el-form-item>
      <!-- 工号栏 -->
      <el-form-item label="工号" prop="workNumber">
        <el-input v-model="formData.workNumber" style="width:400px" placeholder="工号" />
      </el-form-item>
      <!-- 部门栏 -->
      <el-form-item label="部门" prop="departmentName">
        <el-input v-model="formData.departmentName" style="width:400px" placeholder="请选择" @focus="getDepartments" />
        <!-- 引入树形结构 ,导入数据和是否显示，并且引入显示的值，默认为展开样式-->
        <el-tree
          v-if="showtree"
          :data="treeData"
          :props="{label:'name'}"
          default-expand-all=""
          @node-click="selectNode"
        />
      </el-form-item>
      <el-form-item label="转正时间" prop="correctionTime">
        <el-date-picker v-model="formData.correctionTime" placeholder="选择日期" type="date" />
      </el-form-item>
    </el-form>
    <el-row type="flex" justify="center">
      <el-col :span="6">
        <el-button size="small" @click="btnCancel">取消</el-button>
        <el-button type="primary" size="small" @click="btnOK">确定</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>

<script>
// 获取到公司部门
import { getDepartments } from '@/api/department'
// 引入数据转化为树形结构的方法
import { transListToTreeData } from '@/utils'
// 引入枚举组件
import EmployeeEnum from '@/api/constant/employees'
// 引入新增员工的接口
import { addEmployee } from '@/api/employees'
export default {
  // 父组件传入弹层的关闭或显示
  props: {
    showDialog: {
      type: Boolean,
      default: true
    }
  },
  data() {
    return {
      EmployeeEnum,
      // 弹层的表单信息
      formData: {
        username: '', // 用户名
        mobile: '', // 手机号码
        timeOfEntry: '', // 入职时间
        formOfEmployment: '', // 入职的形式
        workNumber: '', // 工号
        departmentName: '', // 部门名字
        correctionTime: '' // 转正时间
      },
      showtree: false, // 用于显示树形结构是否显示
      treeData: [], // 用于放置树形结构的数组
      // 表单的检验
      rules: {
        username: [{ required: true, message: '姓名不能为空', trigger: 'blur' },
          { min: 2, max: 4, message: '名字需要填入2-4个字', trigger: 'blur' }],
        mobile: [{ required: true, message: '手机号码不能为空', trigger: 'blur' },
          { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机格式', trigger: 'blur' }],
        timeOfEntry: [{ required: true, message: '请输入入职时间', trigger: 'blur' }],
        workNumber: [{ required: true, message: '工号不能为空', trigger: 'blur' }],
        departmentName: [{ required: true, message: '请输入部门名称', trigger: 'change' }]
      }
    }
  },
  methods: {
    //   获取部门信息
    async getDepartments() {
      this.showtree = true // 当点击了input窗的时候，让树形结构显示出来
      const { depts } = await getDepartments() // 将得到数据解构放在depts中
      this.treeData = transListToTreeData(depts, '')
      console.log(this.treeData)
    },
    // 将下拉列表赋值给input框
    selectNode(node) {
      this.formData.departmentName = node.name
      this.showtree = false
    },
    // 点击确定按钮时候触发的函数
    async btnOK() {
      try {
        await this.$refs.addEmployee.validate()
        await addEmployee(this.formData)
        this.$parent.getEmployees()
        this.$parent.showDialog = false
      } catch (error) {
        console.log(error)
      }
    },
    // 关闭按钮
    btnCancel() {
      this.formData = {
        username: '', // 用户名
        mobile: '', // 手机号码
        timeOfEntry: '', // 入职时间
        formOfEmployment: '', // 入职的形式
        workNumber: '', // 工号
        departmentName: '', // 部门名字
        correctionTime: '' // 转正时间
      }
      this.$refs.addEmployee.resetFields()
      //   this.$emit('update:showDialog', false)
      this.$emit('closeshowDialog')
    }
  }
}
</script>

<style>

</style>
